package main

import "fmt"

/* 383. 赎金信  https://leetcode.cn/problems/ransom-note/ */

func ExecQ383() {

	fmt.Println(canConstruct("a", "b"))
	fmt.Println(canConstruct("aa", "ab"))
	fmt.Println(canConstruct("aa", "aab"))
}

// 我写的
func canConstruct1(ransomNote string, magazine string) bool {
	if len(magazine) < len(ransomNote) {
		return false
	}
	countChar := func(s string) map[rune]int {
		rst := make(map[rune]int, 0)
		for _, val := range s {
			rst[val] = rst[val] + 1
		}
		return rst
	}
	rMap := countChar(ransomNote)
	mMap := countChar(magazine)
	if len(mMap) < len(rMap) {
		return false
	}
	for val, usage := range rMap {
		if mMap[val] < usage {
			return false
		}
	}
	return true
}

// 官方的 👍
func canConstruct(ransomNote string, magazine string) bool {
	record := make([]int, 26)
	for _, v := range magazine {
		record[v-'a']++
	}
	for _, v := range ransomNote {
		record[v-'a']--
		if record[v-'a'] < 0 {
			return false
		}
	}
	return true
}
